
using Furion.DataEncryption.Extensions;
using Furion.DistributedIDGenerator;

namespace SB2.Core;
[SugarTable("sbsysusers")]
public class User : EntityHasUser
{
    [SugarColumn(IsPrimaryKey = true, ColumnName = "UserID", ColumnDescription = "主键")]
    public long UserID { get; set; }
    [SugarColumn(ColumnDescription = "登录账号", IsNullable = false)]
    public string Name { get; set; }
    [SugarColumn(ColumnDescription = "密码", IsNullable = false)]
    public string Pwd { get; set; }
    [SugarColumn(ColumnDescription = "用户类型")]
    public AdminType AdminType { get; set; }
    [SugarColumn(ColumnDescription = "用户状态")]
    public AdminStatus AdminStatus { get; set; }

    /// <summary>
    /// 默认账号
    /// </summary>
    /// <returns></returns>
    public static User GetDefault()
    {
        return new User { Name = "yes", AdminType = AdminType.SuperAdmin }
    .New().SetPwd("yes123.");
    }

    /// <summary>
    /// 新建一个用户，带id
    /// </summary>
    /// <returns></returns>
    public User New()
    {
        UserID = BitConverter.ToInt64(IDGen.NextID().ToByteArray());
        return this;
    }
    /// <summary>
    /// 验证密码
    /// des 解密后 加上 日期 后md5 加密与传入密码比对
    /// </summary>
    /// <param name="pwd"></param>
    /// <returns></returns>
    public bool CheckePwd(string pwd)
    {
        int span = (int)(DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1)).TotalSeconds;
        // Console.WriteLine(span);
        var p = this.Pwd.ToDESCDecrypt(Encrypt.BaseDESCEncryptKey).ToLower();
        return (p + span).ToMD5Compare(pwd) || (p + (span + 1)).ToMD5Compare(pwd);
    }
    /// <summary>
    /// 设置密码
    /// md5加密后des加密
    /// </summary>
    /// <param name="pwd"></param>
    /// <returns></returns>
    public User SetPwd(string pwd = null)
    {
        var p = pwd ?? this.Pwd + "";
        this.Pwd = p.ToMD5Encrypt().ToDESCEncrypt(Encrypt.BaseDESCEncryptKey);
        return this;
    }
}

public class UserLoginDto
{
    [Required]
    public string Name { get; set; }
    [Required]
    public string Pwd { get; set; }
}

public class UserCreateDto
{
    [Required]
    public string Name { get; set; }
    [Required]
    public AdminStatus AdminStatus { get; set; }
}
public class UserUpdateDto
{
    [Required]
    public long UserID { get; set; }
    public AdminStatus? AdminStatus { get; set; }
    public string Pwd { get; set; }
}

public class UserDto
{

    [DisplayDto(Hide = true)]
    public long UserID { get; set; }
    [DisplayDto(DisplayName = "账号", Seachable = true, DataType = DisplayDtoDataType.Text)]
    public string Name { get; set; }
    public AdminStatus AdminStatus { get; set; }
    [DisplayDto(DisplayName = "状态", FieldName = "AdminStatus", Seachable = true, DataType = DisplayDtoDataType.Enums, DataSourse = "SB2.Core.AdminStatus")]
    public string AdminStatusStr { get { return AdminStatus.GetDescription(); } }
}